#import "PrefsController.h"

@implementation PrefsController

- (IBAction)configureGame:(id)sender
{
   
    sheetIsActive = YES;
    [NSApp beginSheet:configureSheet modalForWindow:mainWindow
        modalDelegate:self didEndSelector:NULL contextInfo:nil];

     [self updatePlugin];
}

- (IBAction)doneConfiguring:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setBool:[modeComputer state] forKey:@"modeComputer"];

    [defaults setFloat:[dificulty floatValue] forKey:@"dificulty"];
    [defaults setFloat:[boardSize floatValue] forKey:@"boardSize"];

    [defaults setBool:[soundEnabled state] forKey:@"soundEnabled"];
    [defaults setBool:[showMoves state] forKey:@"showMoves"];
    
    [defaults setObject:[appearance titleOfSelectedItem] forKey:@"appearance"];

    sheetIsActive = NO;
    
    [plugin setCurrentPlugin:[appearance titleOfSelectedItem]];

    [configureSheet orderOut:nil];
    [NSApp endSheet:configureSheet];
    [gameController prefsChanged];
}

- (IBAction)visitWebsite:(id)sender
{
  NSString *stringURL = @"http://rodesia.org/maciago?from=m36";
  [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:stringURL]];
}

- (void)awakeFromNib
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *prefModeComputer = [defaults stringForKey:@"modeComputer"],
	*prefSoundEnabled = [defaults stringForKey:@"soundEnabled"],
	*prefShowMoves = [defaults stringForKey:@"showMoves"],
	*prefDificulty = [defaults stringForKey:@"dificulty"],
	*prefBoardSize = [defaults stringForKey:@"boardSize"],
	*prefAppearance = [defaults stringForKey:@"appearance"];

    sheetIsActive = NO;

    plugin = [PluginController sharedInstance];
    [plugin setDefaultTitle:[appearance itemTitleAtIndex:0]];
    
    if (prefModeComputer == nil) 
    {
        [modeComputer setState:NSOffState];
        [modeHuman setState:NSOnState];
    }
    else
    {
	[modeComputer setState:[prefModeComputer intValue]];
	[modeHuman setState:![prefModeComputer intValue]];
    }

    if (prefDificulty == nil) 
	[dificulty setFloatValue:3.0];
    else
	[dificulty setFloatValue:[prefDificulty floatValue]];

    if (prefBoardSize == nil) 
	[boardSize setFloatValue:8.0];
    else
	[boardSize setFloatValue:[prefBoardSize floatValue]];

    if (prefSoundEnabled == nil) 
	[soundEnabled setState:NSOnState];
    else
	[soundEnabled setState:[prefSoundEnabled intValue]];

    if (prefShowMoves == nil) 
	[showMoves setState:NSOnState];
    else
	[showMoves setState:[prefShowMoves intValue]];
    
    [self updatePlugin];
    [plugin setCurrentPlugin:prefAppearance];
    [appearance selectItemWithTitle:[plugin currentPlugin]];
}

- (void)updatePlugin
{
    [appearance removeAllItems];
    [appearance addItemsWithTitles:[plugin scanPluginsIn:
         [[NSBundle mainBundle] builtInPlugInsPath] ofType:@"mext"]];
    [appearance selectItemWithTitle:[plugin currentPlugin]];

}


- (BOOL)modeComputer
{
    return [modeComputer state];
}

- (int)dificulty
{
    return [dificulty intValue];
}

- (int)boardSize
{
    return [boardSize intValue];
}

- (BOOL)soundEnabled
{
    return [soundEnabled state];
}

- (BOOL)showMoves
{
    return [showMoves state];
}

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    return !sheetIsActive;
}

- (BOOL)sheetIsActive
{
    return sheetIsActive;
}
@end
